import {defineConfig} from "vite";
import vue from "@vitejs/plugin-vue";
import {loadEnv} from "vite";
import path from "path";

//http://vitejs.dev/config/
// @ts-ignore
export default ({command, mode}) => {

    return defineConfig({
        plugins: [vue()],
        base: './',
        server: {
            host: "127.0.0.1",
            port: Number(loadEnv(mode, process.cwd()).VITE_APP_PORT),
            // port: 8989,
            strictPort: true,  //端口被占用直接推出
            https: false,
            open: false, //默认打开浏览器
            proxy: { //代理配置
                //字符串简写写法
                //'/foo/': ''
                //选项写法
                '/api': {
                    // target: "http://localhost:8080", //代理地址
                    target: loadEnv(mode, process.cwd()).VITE_APP_BASE_URL,
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/api/, "")
                },
            },
            hmr: {
                overlay: false //屏蔽服务器报错
            },
        },
        resolve: { //设置项目文件导入路径
            alias: {
                "@": path.resolve(__dirname, './src')
            }
        },
        css: {
            //css预处理器
            preprocessorOptions: {
                //引入全局的scss文件，这样就可以在全局中使用scss文件中预定义的变量了
                scss: {
                    additionalData: '@import "@/assets/styles/global.scss";'
                }
            }
        },
        build: {  //分块打包配置
            chunkSizeWarningLimit: 1500, //分块打包，分解块，将大块分解为更小的块
            rollupOptions: {
                output: {
                    manualChunks(id) {
                        if (id.includes('node_modules')) {
                            return id.toString().split('node_modules/')[1].split('/')[0].toString();
                        }
                    }
                }
            }
        }
    })
};
